package com.msb.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.msb.common.exception.BizErrorCodeEnum;
import com.msb.common.utils.PageUtils;
import com.msb.common.vo.CommonPageReq;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.entity.product.BrandEntity;
import com.msb.mall.core.valid.groups.AddGroupsInterface;
import com.msb.mall.core.valid.groups.UpdateGroupsInterface;
import com.msb.mall.product.service.BrandService;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 品牌
 *
 * @author leixu
 * @email xuleiwolf@163.com
 * @date 2024-04-19 20:08:58
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @PostMapping("/findAll")
    public CommonResult findAll (@RequestBody Map<String, Object> params) {
        IPage<BrandEntity> page = brandService.findAll(params);

        return CommonResult.success(null, page);
    }

    /**
     * 品牌管理分页展示
     */
    @PostMapping("/queryBrandPage")
    public CommonResult queryBrandPage(@RequestBody CommonPageReq<Map<String, Object>> req){
        PageUtils page = brandService.queryPage(req);

        return CommonResult.success(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public CommonResult info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return CommonResult.success(brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public CommonResult save(@Validated(AddGroupsInterface.class) @RequestBody BrandEntity brand){
        return brandService.saveSingle(brand);

//    public CommonResult save(@Valid @RequestBody BrandEntity brand, BindingResult result){
//        if(result.hasErrors()) {
//            Map<String, Object> map = new HashMap<>();
//            List<FieldError> fieldErrors = result.getFieldErrors();
//            for (FieldError fieldError : fieldErrors) {
//                String field = fieldError.getField();
//                // 获取非法的field的提示信息
//                String defaultMessage = fieldError.getDefaultMessage();
//                map.put(field,defaultMessage);
//            }
//            return CommonResult.error(HttpStatus.SC_BAD_REQUEST, "提交的品牌表单数据不合法", map);
//        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public CommonResult update(@Validated(UpdateGroupsInterface.class) @RequestBody BrandEntity brand){
		return brandService.updateSingle(brand);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody Long[] brandIds){
        CommonResult commonResult = CommonResult.success();

//        brandService.removeByIds(Arrays.asList(brandIds));

        if (brandIds == null || brandIds.length <= 0) {
            commonResult.setCode(BizErrorCodeEnum.VALID_EXCEPTION.getCode());
            commonResult.setMsg("待删除的品牌ID不能为空！");
            return commonResult;
        }

        brandService.deleteBrand(Arrays.asList(brandIds));

        return CommonResult.success();
    }

}
